package com.talking.activities

import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import com.google.gson.JsonParser
import com.parfoismeng.slidebacklib.registerSlideBack
import com.parfoismeng.slidebacklib.unregisterSlideBack
import com.talking.App
import com.talking.R
import com.talking.activities.main.MainActivity
import com.talking.databinding.ActivityLoginBinding
import com.talking.util.Network
import com.talking.util.Tools
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException

class LoginActivity : AppCompatActivity() {
    private val calls: MutableList<Call> = ArrayList()
    private lateinit var launcher: ActivityResultLauncher<Intent>
    private lateinit var binding: ActivityLoginBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.loginLogo.logo.typeface = Typeface.createFromAsset(assets, "font1.TTF")
        binding.loginAutoLogin.setOnClickListener { binding.loginCb.isChecked = !binding.loginCb.isChecked }
        binding.loginForget.setOnClickListener { launcher.launch(Intent(this, ForgetPasswordActivity::class.java), ActivityOptionsCompat.makeSceneTransitionAnimation(this, binding.loginLogo.logo, "logo")) }
        binding.loginRegister.setOnClickListener { launcher.launch(Intent(this, RegisterActivity::class.java), ActivityOptionsCompat.makeSceneTransitionAnimation(this, binding.loginLogo.logo, "logo")) }
        binding.loginLogin.setOnClickListener {
            if (binding.loginMail.text.toString()==""){
                Tools.toast(getString(R.string.input_mail))
                return@setOnClickListener
            }
            if (binding.loginPassword.text.toString()==""){
                Tools.toast(getString(R.string.input_password))
                return@setOnClickListener
            }
            val map = HashMap<String, String>()
            map["mail"] = binding.loginMail.text.toString()
            map["password"] = binding.loginPassword.text.toString()
            Network.post(map, "user/login/") { call: Call ->
                call.enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        e.printStackTrace()
                    }
                    override fun onResponse(call: Call, response: Response) {
                        runOnUiThread{
                            val msg = JsonParser.parseString(response.body!!.string()).asJsonObject.get("msg").asString
                            if (msg.equals("登录成功")){
                                val edit = App.sp.edit();
                                edit.putBoolean("canautologin",binding.loginCb.isChecked)
                                edit.putString("mail",binding.loginMail.text.toString())
                                edit.putString("password",binding.loginPassword.text.toString())
                                edit.apply()
                                launcher.launch(Intent(this@LoginActivity, MainActivity::class.java))
                                finish()
                            }else {
                                Tools.toast(msg)
                            }
                        }
                    }
                })
                calls.add(call)
            }
        }
        launcher = registerForActivityResult(StartActivityForResult()) { result: ActivityResult? ->
            if (result!!.resultCode== 3){//register//forget
                val edit = App.sp.edit();
                edit.putBoolean("canautologin",true)
                edit.putString("mail",result.data!!.getStringExtra("mail"))
                edit.putString("password",result.data!!.getStringExtra("password"))
                edit.apply()
                launcher.launch(Intent(this, MainActivity::class.java))
                finish()
            }
        }
        registerSlideBack { finish() }
    }
    override fun onDestroy() {
        super.onDestroy()
        unregisterSlideBack()
    }
}